package com.example.royalchef.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.royalchef.R;
import com.example.royalchef.activity.PushDetailActivity;
import com.example.royalchef.adaper.PushAdapter;
import com.example.royalchef.base.BaseFragment;
import com.example.royalchef.base.BaseRecyclerViewAdapter;
import com.example.royalchef.bean.PushItem;
import com.example.royalchef.bean.Share;
import com.example.royalchef.view.DividerItemDecoration;
import com.google.gson.Gson;

/**
 * author: Administrator
 * created on: 2017/6/27 9:43
 * description:
 */

public class PushFragment extends BaseFragment {

    private View mInflate;
    private RecyclerView mRecyclerView;
    private LinearLayoutManager manager;
    private DividerItemDecoration decoration;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mInflate = View.inflate(getActivity(), R.layout.fragment_push, null);
        initView();
        initData();
        return mInflate;
    }

    private void initView() {
        mRecyclerView = (RecyclerView) mInflate.findViewById(R.id.rcv_push);
        manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(manager);
        decoration = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST);
        decoration.setHeight(25);
        mRecyclerView.addItemDecoration(decoration);

    }

    private void initData() {
        RequestQueue mQueue = Volley.newRequestQueue(getContext());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://192.168.12.248:8080/message/list.json?page=0&limit=7",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        final Share info = gson.fromJson(response, Share.class);
                        PushAdapter adapter = new PushAdapter(info.data, getContext(), R.layout.item_push);
                        mRecyclerView.setAdapter(adapter);
                        adapter.setOnItemClickLitener(new BaseRecyclerViewAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                Intent intent = new Intent(getContext(), PushDetailActivity.class);
                                Share.Push push = info.data.get(position);
                                PushItem pushItem = new PushItem();
                                pushItem.setTitle(push.title);
                                pushItem.setCover(push.cover);
                                pushItem.setContent(push.content);
                                intent.putExtra("pushitem", pushItem);
                                getContext().startActivity(intent);
                            }
                        });
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        mQueue.add(stringRequest);
    }

}
